

cc.Class({
    extends: cc.Component,

    properties: {
        A :{
            default:null,
            type:cc.Node
        },
        B :{
            default:null,
            type:cc.Node
        },
        C :{
            default:null,
            type:cc.Node
        },
        D :{
            default:null,
            type:cc.Node
        },
        E :{
            default:null,
            type:cc.Node
        },
        F :{
            default:null,
            type:cc.Node
        },
        G :{
            default:null,
            type:cc.Node
        },
        A1 :{
            default :null,
            type : cc.Node
        },
        A2 :{
            default :null,
            type : cc.Node
        },
        A3 :{
            default :null,
            type : cc.Node
        },
        A4 :{
            default :null,
            type : cc.Node
        },
        A5 :{
            default :null,
            type : cc.Node
        },
        B1 :{
            default :null,
            type : cc.Node
        },
        B2 :{
            default :null,
            type : cc.Node
        },
        B3 :{
            default :null,
            type : cc.Node
        },
        B4 :{
            default :null,
            type : cc.Node
        },
        B5 :{
            default :null,
            type : cc.Node
        },
        C1 :{
            default :null,
            type : cc.Node
        },
        C2 :{
            default :null,
            type : cc.Node
        },
        C3 :{
            default :null,
            type : cc.Node
        },
        C4 :{
            default :null,
            type : cc.Node
        },
        C5 :{
            default :null,
            type : cc.Node
        },
        D1 :{
            default :null,
            type : cc.Node
        },
        D2 :{
            default :null,
            type : cc.Node
        },
        D3 :{
            default :null,
            type : cc.Node
        },
        D4 :{
            default :null,
            type : cc.Node
        },
        D5 :{
            default :null,
            type : cc.Node
        },
        E1 :{
            default :null,
            type : cc.Node
        },
        E2 :{
            default :null,
            type : cc.Node
        },
        E3 :{
            default :null,
            type : cc.Node
        },
        E4 :{
            default :null,
            type : cc.Node
        },
        E5 :{
            default :null,
            type : cc.Node
        },
        F1 :{
            default :null,
            type : cc.Node
        },
        F2 :{
            default :null,
            type : cc.Node
        },
        F3:{
            default :null,
            type : cc.Node
        },
        F4:{
            default :null,
            type : cc.Node
        },
        F5:{
            default :null,
            type : cc.Node
        },
        G1 :{
            default :null,
            type : cc.Node
        },
        G2 :{
            default :null,
            type : cc.Node
        },
        G3:{
            default :null,
            type : cc.Node
        },
        G4:{
            default :null,
            type : cc.Node
        },
        G5:{
            default :null,
            type : cc.Node
        },
    },
    

   
    onLoad() {
        var params = {
            depth : cc.vv.userMgr.depth,
            user_id : cc.vv. userMgr.userid
        }
        let self = this
        cc.vv.http.sendRequest("localGrand",params,function(data) {
            
            self.changeAllPics(data.data.diamonds)
            
        },cc.vv.url.diamondServer)

        self.pdPage()
        
        
        
    },
    
    changePic: function(type, nodeName) {
        var node = this[nodeName];
        
        if (!node) {
            console.error("节点不存在:", nodeName);
            return;
        }

        cc.loader.loadRes("textures/images/grand/diamond/"+type, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                console.error("载入图片资源失败:", err);
                return;
            }
            var sprite = node.getComponent(cc.Sprite);
            if (sprite) {
                sprite.spriteFrame = spriteFrame;
                
            } 
        }.bind(this));
    },

    changeAllPics: function(data) {
        let i = 0
        let j = 0
        let arr = ['A','B','C','D','E','F','G']
        for (var nodeName in this) {
            
            if (arr.indexOf(nodeName ) != -1) {
                continue
            }
            // 确保属性是节点类型并且不是函数
            if (this[nodeName] instanceof cc.Node ) {
                
                if(cc.vv.userMgr.depth==0) {
                    type = 0
                }else {
                    var type = data[i][j].diamond_state
                }
                
                
                switch (type) {
                    case 0:
                        type = "soil"
                        break;
                    case 1:
                        type = "iron"
                        break;
                    case 2:
                        type = "stone"
                        break;
                    case 3:
                        type = "gold"
                        break;
                    case 4:
                        type = "diamond"
                        break;
                    case 5 :
                        type = "soilBackground"
                        break;
                    
                    default:
                        console.log("无效的类型");
                }
                
                this.changePic(type, nodeName);
                cc.log(type)
                j++
                if (j==5) {
                    i++
                    j=0
                }
                if(i==7) {
                    break
                }
            }
            
        }
        
        
    },
    
    grandDown() {
        depth = cc.vv.userMgr.depth
        var params = {
            depth : cc.vv.userMgr.depth,
            user_id : cc.vv. userMgr.userid
        }
        var self = this 
        cc.vv.http.sendRequest("grandDown",params,function(data) {
            
            self.changeAllPics(data.data.diamonds)
            
        },cc.vv.url.diamondServer)
        
        if(cc.vv.userMgr.depth == 0) {
            cc.vv.userMgr.depth += 5
        }else {
            cc.vv.userMgr.depth += 7
        }
        
    },


    grandUp() {
        depth = cc.vv.userMgr.depth
        if (depth ==0) {
            cc.log("已经在最上方")
        }else {
            var params = {
                depth : cc.vv.userMgr.depth,
                user_id : cc.vv. userMgr.userid
            }
            var self = this
            cc.vv.http.sendRequest("grandUp",params,function(data) {
                
                self.changeAllPics(data.data.diamonds)
            },cc.vv.url.diamondServer) 
            if(cc.vv.userMgr.depth == 5) {
                cc.vv.userMgr.depth = 0
            }else {
                cc.vv.userMgr.depth-= 7
            }
        }
        
        
    },

    pdPage() {
        var label = cc.find("Canvas/info/depth/depthNum");
        
        if (label.getComponent(cc.Label)) {
            label.getComponent(cc.Label).string = cc.vv.userMgr.depth;
        }
        if(cc.vv.userMgr.depth <= 4) {
            
            this.A.active = false;
            this.B.active = false;
            this.C.active = false;
            
        }else {
            this.A.active = true;
            this.B.active = true;
            this.C.active = true;
            
        }
    },
    start () {

    },

    
});
